# 数据处理相关模块
import json
import csv


def jdwx_price_process(file):
    def price_processor(item):
        if len(item['price']) == 0:
            item['price'] = [[1, 0]]
        else:
            if '单个' in item['price'][0][0]:
                item['price'] = [[1, item['price'][0][1]]]
            else:
                for index, price in enumerate(item['price']):
                    temp = price[0].replace('万', '0000').split('次≈')
                    if len(temp) == 2:
                        item['price'][index] = [int(temp[0]), price[1]]
                    else:
                        # 这里匹配到的是按照时间收费的API，如一个月多少钱，一个季度多少钱等，无需进行处理
                        pass
        return item

    with open(file, 'r') as f:
        jdwx_app_info = json.load(f)
    jdwx_app_info = filter(lambda x: x != 'Error', jdwx_app_info)
    jdwx_app_info = list(map(price_processor, jdwx_app_info))
    return jdwx_app_info


def tencent_price_process(file):
    def price_processor(item):
        for index, price in enumerate(item['price']):
            temp = price['spec'].replace(',', '').split('元/')
            item['price'][index] = [int(temp[1].replace('次', '')), float(temp[0])]
        return item

    with open(file, 'r') as f:
        tencent_app_info = json.load(f)
    tencent_app_info = filter(lambda x: x != 'Error', tencent_app_info)
    # deliver 为交付方式，分为：
    # MANUAL	6092    手动
    # SAAS	    4579    SaaS交付
    # IMAGE	    682 	镜像交付
    # API	    663 	API交付
    # VOUCHER	75 	    兑换码交付
    # 仅收集其中API交付部分，手动交付大部分均为服务而非数据资产
    tencent_app_info = filter(lambda x: x['deliver'] == 'API', tencent_app_info)
    tencent_app_info = list(map(price_processor, tencent_app_info))
    return tencent_app_info


def to_csv(file, app_info, data_type):
    field_names = {
        'jdwx': ['url', 'title', 'tags', 'intro', 'rating', 'label', 'shop', 'view', 'buy', 'favorite', 'comment',
                 'avg'],
        'tencent': ['url', 'title', 'intro', 'rating', 'label_url', 'shop', 'buy', 'comment', 'avg']
    }

    def data_format(item):
        if item['tags'] is not None:
            item['tags'] = item['tags'][0]
        item['intro'] = item['intro'].replace('\n', '')
        avg = 0
        for index, price in enumerate(item['price']):
            item['price' + str(index)] = price[1]
            item['times' + str(index)] = price[0]
            if type(price[0]) != str:
                avg += price[1] / price[0]
        item['avg'] = avg
        return item

    app_info = list(map(data_format, app_info))
    length = max([len(item['price']) for item in app_info])
    # 为兼容 MS Office Excel 设置编码为 utf-8 with BOM，即utf-8-sig编码
    with open(file, 'w', newline='', encoding='utf-8-sig') as f:
        field_name = field_names[data_type]
        for i in range(length):
            field_name.append('times' + str(i))
            field_name.append('price' + str(i))

        writer = csv.DictWriter(f, fieldnames=field_name, extrasaction='ignore')
        writer.writeheader()
        writer.writerows(app_info)


def get_analysis(csv_file, html_file):
    import pandas as pd
    import pandas_profiling

    titanic = pd.read_csv(csv_file)
    pfr = pandas_profiling.ProfileReport(titanic)
    pfr.to_file(html_file)
